package org.finesys.system.service.tenant.impl;

import org.finesys.common.constants.BaseConstant;
import org.finesys.common.core.exception.BusinessException;
import org.finesys.common.mp.core.service.impl.BaseServiceImpl;
import org.finesys.system.api.tenant.SysTenant;
import org.finesys.system.mapper.tenant.SysTenantMapper;
import org.finesys.system.service.tenant.SysTenantService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;


@Service
public class SysTenantServiceImpl extends BaseServiceImpl<SysTenantMapper, SysTenant> implements SysTenantService {

    /**
     * 校验租户信息是否合法
     *
     * @param id 租户id
     */
    @Override
    public void validTenant(Long id) {
        //获取租户
        SysTenant sysTenant = baseMapper.selectById(id);
        if (Objects.isNull(sysTenant)) {
            throw new BusinessException("租户信息不存在!");
        }
        if (BaseConstant.STATUS_UN_VALID.equals(sysTenant.getStatus())) {
            throw new BusinessException(String.format("租户未开启:%s!", sysTenant.getName()));
        }

        if(sysTenant.getExpiredTime()!=null){
            if (LocalDateTime.now().isAfter(sysTenant.getExpiredTime())) {
                throw new BusinessException(String.format("租户已过期:%s!", sysTenant.getName()));
            }
        }
    }
}
